/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VSLangProj80;
using System.IO;

namespace DaveSexton.DocProject.Sandcastle
{
	public sealed partial class ChoosePresentationControl : UserControl
	{
		#region Public Properties
		public Configuration.Presentation SelectedPresentation
		{
			get
			{
				return (presentationList.SelectedIndex > -1)
					? ((PresentationListItem) presentationList.SelectedItem).Element
					: null;
			}
		}

		public int PresentationCount
		{
			get
			{
				EnsurePresentationList();

				return presentationList.Items.Count;
			}
		}
		#endregion

		#region Private / Protected
		private bool presentationListFilled;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChoosePresentationControl" /> class.
		/// </summary>
		public ChoosePresentationControl()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		private void EnsurePresentationList()
		{
			if (presentationListFilled)
				return;

			presentationList.SuspendLayout();

			PresentationListItem defaultItem = null;

			for (int i = 0; i < SandcastleBuildEngineProvider.Presentations.Count; i++)
			{
				Configuration.Presentation element = SandcastleBuildEngineProvider.Presentations[i];

				if (!Directory.Exists(Path.Combine(SandcastleBuildEngineProvider.Paths.Presentation, element.Path)))
					continue;

				PresentationListItem item = new PresentationListItem(element);

				if (element == SandcastleBuildEngineProvider.DefaultPresentation)
					defaultItem = item;

				presentationList.Items.Add(item);
			}

			if (defaultItem != null)
				presentationList.SelectedItem = defaultItem;

			presentationList.ResumeLayout(true);

			presentationListFilled = true;
		}
		#endregion

		#region Event Handlers
		protected override void OnLoad(EventArgs e)
		{
			EnsurePresentationList();

			base.OnLoad(e);
		}

		private void presentationList_SelectedIndexChanged(object sender, EventArgs e)
		{
			Configuration.Presentation element = SelectedPresentation;

			descriptionLabel.Text = (element == null) ? string.Empty : element.Description;
		}

		private void descriptionLabel_Paint(object sender, PaintEventArgs e)
		{
			using (System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
				descriptionLabel.ClientRectangle, Color.White, Color.AntiqueWhite, System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal))
			{
				e.Graphics.FillRectangle(brush, descriptionLabel.ClientRectangle);

				TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter
					| TextFormatFlags.WordBreak | TextFormatFlags.LeftAndRightPadding;

				if (RenderDescriptionLabelRightToLeft())
					flags |= TextFormatFlags.RightToLeft;

				TextRenderer.DrawText(e.Graphics, descriptionLabel.Text, descriptionLabel.Font, descriptionLabel.ClientRectangle, 
					descriptionLabel.ForeColor, Color.Transparent, flags);
			}
		}

		private bool RenderDescriptionLabelRightToLeft()
		{
			Control control = descriptionLabel;

			do
			{
				switch (control.RightToLeft)
				{
					case RightToLeft.Yes:
						return true;
					case RightToLeft.No:
						return false;
					default:
						control = control.Parent;
						break;
				}
			}
			while (control != null);

			return false;
		}
		#endregion

		#region Nested
		private sealed class PresentationListItem
		{
			#region Public Properties
			public Configuration.Presentation Element { get { return element; } }
			#endregion

			#region Private / Protected
			private readonly Configuration.Presentation element;
			#endregion

			#region Constructors
			/// <summary>
			/// Constructs a new instance of the <see cref="PresentationListItem" /> class.
			/// </summary>
			public PresentationListItem(Configuration.Presentation element)
			{
				this.element = element;
			}
			#endregion

			#region Methods
			public override string ToString()
			{
				return element.Name;
			}
			#endregion
		}
		#endregion
	}
}
